package com.sichen.product.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sichen.common.utils.PageUtils;
import com.sichen.common.utils.R;
import com.sichen.product.entity.CategoryBrandRelationEntity;
import com.sichen.product.service.CategoryBrandRelationService;
import com.sichen.product.vo.BrandRelationRespVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * 品牌分类关联
 *
 * @author sichen
 * @email 15837757959@163.com
 * @date 2022-05-14 14:44:50
 */
@RestController
@RequestMapping("product/categorybrandrelation")
@Api(tags = "商品品牌分类关联关系")
public class CategoryBrandRelationController {
    @Resource
    private CategoryBrandRelationService categoryBrandRelationService;




    /**
     * 查询商品品牌和分类的关联列表
     * @param params 分页的条件
     * @return 分页后的数据
     */
    @GetMapping("/list")
    //@RequiresPermissions("produck:categorybrandrelation:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = categoryBrandRelationService.queryPage(params);
        return R.ok().put("page", page);
    }


    /**
     * 根据品牌id查询所有品牌分类的关联信息集合
     * @param brandId 品牌id
     * @return 品牌和分类的关系集合
     */
    @GetMapping("/catelog/list")
    @ApiOperation(value="根据品牌ID查询品牌和分裂的关联关系")
    //@RequiresPermissions("produck:categorybrandrelation:list")
    public R catelogList(@RequestParam("brandId") Long brandId){
        List<CategoryBrandRelationEntity> data = categoryBrandRelationService.list(
            new QueryWrapper<CategoryBrandRelationEntity>().eq("brand_id",brandId)
        );
        return R.ok().put("data", data);
    }

    /**
     * 查询详细的品牌的数据
     * @param id 品牌id
     * @return 详细的品牌的数据
     */
    @GetMapping("/info/{id}")
    //@RequiresPermissions("produck:categorybrandrelation:info")
    public R info(@PathVariable("id") Long id){
		CategoryBrandRelationEntity categoryBrandRelation = categoryBrandRelationService.getById(id);
        return R.ok().put("categoryBrandRelation", categoryBrandRelation);
    }

    /**
     * 保存分类和品牌的关联关系
     * @param categoryBrandRelation 分类和品牌的关联关系
     * @return 返回保存是否成功
     */
    @PostMapping("/save")
    //@RequiresPermissions("produck:categorybrandrelation:save")
    public R save(@RequestBody CategoryBrandRelationEntity categoryBrandRelation){
		categoryBrandRelationService.saveDetail(categoryBrandRelation);
        return R.ok();
    }

    /**
     * 修改(品牌和分类)的关联信息
     * @param categoryBrandRelation 关联信息
     * @return 修改是否成功
     */
    @PutMapping("/update")
    //@RequiresPermissions("produck:categorybrandrelation:update")
    public R update(@RequestBody CategoryBrandRelationEntity categoryBrandRelation){
		categoryBrandRelationService.updateById(categoryBrandRelation);
        return R.ok();
    }

    /**
     * 批量删除关联信息
     * @param ids 关联关系的id
     * @return 删除是否成功
     */
    @DeleteMapping("/delete")
    //@RequiresPermissions("produck:categorybrandrelation:delete")
    public R delete(@RequestBody Long[] ids){
		categoryBrandRelationService.removeByIds(Arrays.asList(ids));
        return R.ok();
    }


    /**
     * 根据分类id查询所有关联的品牌id&名称
     * @param catId 分类id
     * @return 封装的数据
     */
    @GetMapping("/brands/list")
    public R brandsByList(@RequestParam(value = "catId" , required = true) Long catId){
        List<CategoryBrandRelationEntity> list = categoryBrandRelationService.list(
                new QueryWrapper<CategoryBrandRelationEntity>().eq("catelog_id",catId));
        List<BrandRelationRespVo> collect = list.stream().map((item) -> {
            BrandRelationRespVo brandRelationRespVo = new BrandRelationRespVo();
            brandRelationRespVo.setBrandId(item.getBrandId());
            brandRelationRespVo.setBrandName(item.getBrandName());
            return brandRelationRespVo;
        }).collect(Collectors.toList());
        return R.ok().put("data",collect);
    }
}
